#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;


//void runChild()
//{
//    int count = 5;
//    while(count)
//    {
//        printf("I am Child Process, pid: %d, ppid: %d\n", getpid(), getppid());
//        sleep(1);
//        count--;
//    }
//}

#define TASK_NUM 10

typedef void(*task_t)();
task_t tasks[TASK_NUM];

void task1()
{
    printf("这是一个执行打印日志的任务, pid: %d\n", getpid());
}

void task2()
{
    printf("这是一个执行检测网络健康状态的任务, pid: %d\n", getpid());
}

void task3()
{
    printf("这是一个执行绘制图形界面的任务, pid: %d\n", getpid());
}

int AddTask(task_t t)
{
    int pos = 0;
    for(; pos < TASK_NUM; pos++)
    {
        if(!tasks[pos])
            break;
    }
    if(pos == TASK_NUM)
        return -1;
    tasks[pos] = t;
    return 0;
}

void InitTask()
{
    for(int i = 0; i < TASK_NUM; i++)
        tasks[i] = NULL;

    AddTask(task1);
    AddTask(task2);
    AddTask(task3);
}

bool DelTask(int pos)
{
    if(!tasks[pos] || pos >= TASK_NUM)
        return false;

    tasks[pos] = NULL;
    return true;
}

bool CheckTask(int pos)
{
    if(!tasks[pos] || pos >= TASK_NUM)
        return false;

    return true;
}

bool UpdateTask(int pos, task_t t)
{
    if(pos >= TASK_NUM)
        return false;

    tasks[pos] = t;
    return true;
}

void ExecuteTask()
{
    for(int pos = 0; pos < TASK_NUM; pos++)
    {
        if(!tasks[pos])
            continue;
        tasks[pos]();
    }
}


int main()
{
    //for(int i = 0; i < 10; i++)
    //{
    //    pid_t id = fork();
    //    if(id == 0)
    //    {
    //        runChild();
    //        exit(0);
    //    }
    //    printf("create Chile Process: %d success\n", id);
    //}
    //
    //sleep(10);

    //for(int i = 0; i < 10; i++)
    //{
    //    pid_t id = wait(NULL);
    //    if(id > 0)
    //    {
    //        printf("wait %d success\n", id);
    //    }
    //}

    //sleep(10);

    pid_t id = fork();
    if(id == 0)
    {
        // child
        //int* p = NULL;
        int count = 5;
        while(count)
        {
            printf("I am child, pid: %d, ppid: %d, count: %d\n", getpid(), getppid(), count);
            sleep(1);
            count--;
            //*p = 100;
        }
        exit(0);
    }
    else if(id > 0)
    {
       // parent
       //int count = 10;
       //while(count)
       //{
       //     printf("I am father, pid: %d, ppid: %d, count: %d\n", getpid(), getpid(), count);
       //     sleep(1);
       //     count--;
       //}
        
        InitTask();
        int status = 0;
        //pid_t ret = wait(NULL);
        //pid_t ret = waitpid(id, &status, 0);
        while(true) // 轮询
        {
            pid_t ret = waitpid(id, &status, WNOHANG); // 非阻塞
            if(ret > 0)
            {
                //printf("wait success, ret: %d, exit sig: %d, exit code: %d\n", ret, status&0x7f, (status>>8)&0xff);
                if(WIFEXITED(status))
                {
                    printf("进程正常，退出码: %d\n", WEXITSTATUS(status));
                }
                else
                {
                    printf("进程异常\n");
                }
                break;
            }
            else if(ret < 0)
            {
                printf("wait failed\n");
                break;
            }
            else 
            {
                //printf("子进程还未结束\n");
                //sleep(1);
                ExecuteTask();
                usleep(500000);
            }
        }
        sleep(3);
    }
    else
    {
        // error
        perror("fork");
        return 1;
    }

    return 0;
}
